package com.example.netty.tcp.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {
    public static Map<String, String> readExcel(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            System.err.println("错误：文件不存在！路径：" + filePath);
            return new HashMap<>();
        }

        Map<String, String> signalMap = new HashMap<>();
        try (FileInputStream fis = new FileInputStream(file);
             XSSFWorkbook workbook = new XSSFWorkbook(fis)) {

            XSSFSheet sheet = workbook.getSheetAt(2); // 第三个工作表

            for (Row row : sheet) {
                if (row.getRowNum() < 2) continue; // 跳过表头

                Cell idCell = row.getCell(9); // 信号量ID列
                Cell nameCell = row.getCell(1); // 信号标准名列

                if (idCell != null && nameCell != null) {
                    String signalId = idCell.getStringCellValue();
                    String signalName = nameCell.getStringCellValue();
                    signalMap.put(signalId, signalName);
                }
            }
        } catch (IOException e) {
            System.err.println("读取Excel失败：" + e.getMessage());
            e.printStackTrace();
        }
        return signalMap;
    }
}
